package com.tiancheng.trade.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.payment.mapper.OrgPaymentChannelMapper;
import com.tiancheng.trade.payment.mapper.OrgPaymentChannelWayMapper;
import com.tiancheng.trade.payment.mapper.PaymentWayMapper;
import com.tiancheng.trade.payment.model.OrgPaymentChannel;
import com.tiancheng.trade.payment.model.OrgPaymentChannelWay;
import com.tiancheng.trade.payment.model.PaymentWay;
import com.tiancheng.trade.payment.service.OrgPaymentChannelWayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author: kellen
 * @Description:
 * @Date: create in 2024/12/4 17:02
 */
@Service
public class OrgPaymentChannelWayServiceImpl extends ServiceImpl<OrgPaymentChannelWayMapper, OrgPaymentChannelWay> implements OrgPaymentChannelWayService {

    @Resource
    private OrgPaymentChannelMapper orgPaymentChannelMapper;
    @Resource
    private PaymentWayMapper paymentWayMapper;

    @Override
    public boolean newWay(OrgPaymentChannelWay entity) {

        final long count = this.count(new LambdaQueryWrapper<OrgPaymentChannelWay>().eq(OrgPaymentChannelWay::getOrgPaymentChannelId, entity.getOrgPaymentChannelId())
                .eq(OrgPaymentChannelWay::getPaymentWayId, entity.getPaymentWayId()));
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(count==0,"不允许添加重复的支付方式");

        final OrgPaymentChannel orgPaymentChannel = orgPaymentChannelMapper.selectById(entity.getOrgPaymentChannelId());
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(Objects.nonNull(orgPaymentChannel),"机构支付通道不存在！");

        final PaymentWay paymentWay = paymentWayMapper.selectById(entity.getPaymentWayId());
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(Objects.nonNull(paymentWay),"支付方式不存在！");

        entity.setStopUsing(0);
        return this.save(entity);
    }
}
